<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Email Validation</title>
    <style>
        .input-container {
            position: relative;
        }
        
        input {
            padding: 10px;
            border: 2px solid #ccc;
            transition: border-color 0.3s ease;
        }
        
        .valid {
            border-color: green;
        }
        
        .invalid {
            border-color: red;
        }
    </style>
</head>

<body>
    <div class="input-container">
        <input type="text" id="emailInput" />
    </div>
    <script>
        // 获取元素
        const emailInput = document.getElementById('emailInput')

        // 注册 input
        emailInput.addEventListener('input', function() {
            // 正则
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
                // 校验
            if (emailRegex.test(this.value)) {
                this.classList.add('valid')
                    // 通过移除和添加类名实现的功能
                this.classList.remove('invalid')
            } else {
                this.classList.add('invalid')
                this.classList.remove('valid')
            }
        })
    </script>
</body>

</html>